package cn.stylefeng.guns.modular.ums.service.impl;

import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.ums.entity.ChatHistory;
import cn.stylefeng.guns.modular.ums.mapper.ChatHistoryMapper;
import cn.stylefeng.guns.modular.ums.model.params.ChatHistoryParam;
import cn.stylefeng.guns.modular.ums.model.result.ChatHistoryResult;
import  cn.stylefeng.guns.modular.ums.service.ChatHistoryService;
import cn.stylefeng.guns.utils.json.ValidatorUtils;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 聊天记录表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2022-05-17
 */
@Service
public class ChatHistoryServiceImpl extends ServiceImpl<ChatHistoryMapper, ChatHistory> implements ChatHistoryService {

    @Transactional
    @Override
    public void add(List<ChatHistoryParam> param) {
        for (ChatHistoryParam item:param) {
            item.setSendTime(new Date(item.getTimestamp()));
            //进行转换
            if (ValidatorUtils.notEmpty(item.getPayload())){
                ChatHistoryParam.Payload payload= JSONUtil.toBean(item.getPayload(), ChatHistoryParam.Payload.class);
                if ("text".equals(item.getType())){
                    item.setText(payload.getText());
                }else if ("image".equals(item.getType())){
                    item.setName(payload.getName());
                    item.setUrl(payload.getUrl());
                    item.setWidth(payload.getWidth());
                    item.setHeight(payload.getHeight());
                    item.setSize(payload.getSize());
                }else if ("audio".equals(item.getType())){
                    item.setName(payload.getName());
                    item.setUrl(payload.getUrl());
                    item.setDuration(payload.getDuration());
                    item.setSize(payload.getSize());
                }else if ("video".equals(item.getType())){
                    item.setName(payload.getVideo().getName());
                    item.setUrl(payload.getVideo().getUrl());
                    item.setDuration(payload.getVideo().getDuration());
                    item.setSize(payload.getVideo().getSize());
                    item.setCoverUrl(payload.getThumbnail().getUrl());
                }
            }
            this.add(item);
        }
    }

    @Override
    public void add(ChatHistoryParam param){
        ChatHistory entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ChatHistoryParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ChatHistoryParam param){
        ChatHistory oldEntity = getOldEntity(param);
        ChatHistory newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ChatHistoryResult findBySpec(ChatHistoryParam param){
        return null;
    }

    @Override
    public List<ChatHistoryResult> findListBySpec(ChatHistoryParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ChatHistoryParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ChatHistoryParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ChatHistory getOldEntity(ChatHistoryParam param) {
        return this.getById(getKey(param));
    }

    private ChatHistory getEntity(ChatHistoryParam param) {
        ChatHistory entity = new ChatHistory();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
